﻿/***********************************************
 * CodeList.2
 * 文章：C# 中的委托(Delegate)
 * 出处：http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-In-CSharp.aspx
 * 日期：2007-9-20
 * 说明：本范例演示使用委托的一个简单例子
 * 作者：张子阳(Jimmy Zhang)
 * ********************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace Delegate {

	public delegate void GreetingDelegate(string name);

	class Program {
		private static void GreetPeople(string name, GreetingDelegate MakeGreeting) {
			MakeGreeting(name);
		}

		private static void EnglishGreeting(string name) {
			Console.WriteLine("Morning, " + name);
		}

		private static void ChineseGreeting(string name) {
			Console.WriteLine("早上好, " + name);
		}

		static void Main(string[] args) {
			GreetingDelegate delegate1 = new GreetingDelegate(EnglishGreeting);
			delegate1 += ChineseGreeting;	// 给此委托变量再绑定一个方法

			// 将先后调用 EnglishGreeting 与 ChineseGreeting 方法
			GreetPeople("Jimmy Zhang", delegate1);
			Console.WriteLine();

			delegate1 -= EnglishGreeting; //取消对EnglishGreeting方法的绑定
			// 将仅调用 ChineseGreeting 
			GreetPeople("张子阳", delegate1);
			Console.ReadKey();

		}
	}
}
